import 'dart:io';

import 'package:flutter/services.dart';

import 'flutter_douyin.dart';

typedef Success<T> = Function(T data);
typedef Fail = Function(int code, String msg);

class FlutterDouYinAuth {
  static late EventChannel _eventChannel;

  /**
   * shareType
   */
  static final String DouyinOpenSDKShareTypeCapture = "Capture";
  static final String DouyinOpenSDKShareMediaType = "mediaType";

  /**
   * mediaType
   */
  static final String DouyinOpenSDKShareMediaTypeImage = "Image";
  static final String DouyinOpenSDKShareMediaTypeVideo = "Video";

  /**
   * landedPageType
   */
  static final String DouyinOpenSDKLandedPageClip = "Clip";
  static final String DouyinOpenSDKLandedPageEdit = "Edit";
  static final String DouyinOpenSDKLandedPagePublish = "Publish";

  /**
   * 初始化平台
   */
  static Future<String> initPlatform(String androidkey, String ioskey) async {
    return await FlutterDouyin.setKey(androidkey, ioskey) ??
        'Unknown platform message';
  }

  /**
   * 分享抖音视频
   *
   * 1
   * shareType 取值  Capture 打开抖音录制   mediaType 打开相册
   * 必填 shareId 分享的ID
   * 选填 hashtag 话题
   *
   * 2
   * 当 shareType = mediaType 时
   * 必填 media(localIdentifiers) 设置分享的资源标示 List<String>
   * 必填  mediaType  打开类型  Image(DouyinOpenSDKShareMediaTypeImage) 图片  Video(DouyinOpenSDKShareMediaTypeVideo) 视频
   * 必填 landedPageType 设置分享的目标页面  Clip(DouyinOpenSDKLandedPageClip) 剪辑界面    Edit(DouyinOpenSDKLandedPageEdit) 编辑界面  Publish(DouyinOpenSDKLandedPagePublish) 发布页面 现在支持但视频（需要抖音14.7版本以上）
   */
  static Future douyinOpenSDKShareRequest(
      String shareType,
      String hashtag,
      String shareId,
      String mediaType,
      String landedPageType,
      String localIdentifiers,
      {required Success success,
      required Fail fail}) async {
    Map<String, dynamic> arguments = {};
    arguments['hashtag'] = hashtag;
    arguments['state'] = shareId;

    if (shareType == null) {
      shareType = DouyinOpenSDKShareTypeCapture;
    }

    if (shareType.endsWith(DouyinOpenSDKShareTypeCapture)) {
    } else if (shareType.endsWith(DouyinOpenSDKShareMediaType)) {
      arguments['media'] = localIdentifiers;
      arguments['mediaType'] = mediaType;
      arguments['landedPageType'] = landedPageType;
    } else {
      return;
    }

    String results = await FlutterDouyin.DouyinOpenSDKShareRequest(arguments);
    if (results.endsWith("success")) {
      if (Platform.isAndroid) {
        //安卓平台
        _eventChannel = const EventChannel('jldt_dy_flutter');
        _eventChannel.receiveBroadcastStream().listen((event) {
          print("event-1: $event");
          success(event);
        });
      } else if (Platform.isIOS) {
        // IOS 平台
        FlutterDouyin.authUpdate.listen((event) {
          print("event-2: $event");
          success(event);
        });
      }
    } else {
      fail(-1, results);
    }
  }

  /**
   * 请求授权
   */
  static Future douyinOpenSDKAuthRequest(
      List<String> permissionMest, List<String> permissionOptional,
      {required Success success, required Fail fail}) async {
    //List<Dynamic>
    if (permissionMest.isEmpty) {
      permissionMest.add("user_info");
    }

    // List<List<String>> permissions = [];

    Map<String, List<String>> permissions = {};
    permissions['permissionMest'] = permissionMest;
    // permissions.(permissionMest);
    if (permissionOptional != null) {
      if (permissionOptional.isNotEmpty) {
        permissions['permissionOptional'] = permissionOptional;
      }
    }

    String results = await FlutterDouyin.DouyinOpenSDKAuthRequest(permissions);
    if (results.endsWith("success")) {
      if (Platform.isAndroid) {
        //安卓平台
        _eventChannel = const EventChannel('jldt_dy_flutter');
        _eventChannel.receiveBroadcastStream().listen((event) {
          print("event-1: $event");
          success(event);
        });
      } else if (Platform.isIOS) {
        // IOS 平台
        FlutterDouyin.authUpdate.listen((event) {
          print("event-2: $event");
          success(event);
        });
      }
    } else {
      fail(-1, results);
    }
  }
}
